﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using ENAMonitor.ServiceReference1;

namespace ENAMonitor
{
    public partial class Form1 : Form
    {
        ViewENAStateSoapClient vsc = new ViewENAStateSoapClient("ViewENAStateSoap");
        public static ENAInfo[] eNAs;
        public static List<ENAInfo> eNAInfos;
        public static int pcount, fcount;
        private static int i = 0;
        private BackgroundWorker backgroundWorker1;
        
        public Form1()
        {
            InitializeComponent();
            backgroundWorker1 = new BackgroundWorker();
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
        }

        void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                GetData();
            }
            catch
            {
                e.Cancel = true;
                MessageBox.Show("连接服务器失败，无法获取数据，请检查并重启软件！");
            }
        }

        void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                this.timer1.Enabled = false;
                NoBindIcon();
            }
            else if (e.Cancelled)
            {
                this.timer1.Enabled = false;
                NoBindIcon();
            }
            else
            {
                BuildData();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
            this.timer1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.timer1.Enabled = false;
            NoBindIcon();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        private void GetData()
        {
            eNAs = vsc.ENAMonitor(out pcount, out fcount);
        }

        private void BuildData()
        {
            this.label1.Text = pcount.ToString();
            this.label2.Text = fcount.ToString();
            this.label5.Text = "最近刷新时间：" + DateTime.Now.ToLongTimeString();
            eNAInfos = new List<ENAInfo>();
            foreach (ENAInfo ei in eNAs)
            {
                eNAInfos.Add(ei);
            }
            BindENAs(eNAInfos);
            BindImage(fcount);
        }

        private void BindImage(int f)
        {
            if (f > 0)
            {
                this.timer2.Enabled = true;
                this.pictureBox1.ImageLocation = Application.StartupPath + "\\RedBall.png";
            }
            else
            {
                NoBindIcon();
                this.pictureBox1.ImageLocation = Application.StartupPath + "\\GreenBall.png";
            }
        }

        private void BindENAs(List<ENAInfo> list)
        {
            this.dataGridView1.DataSource = list;
            this.dataGridView1.Columns.Clear();
            DisplayCol(dataGridView1, "ENAID", "矢网编号", true);
            DisplayCol(dataGridView1, "ENAPlace", "矢网位置", true);
            DisplayCol(dataGridView1, "ENAIP", "矢网IP", true);
            DisplayCol(dataGridView1, "ENAState", "矢网状态", true);
            //this.dataGridView1.AutoGenerateColumns = false;
        }

        private void DisplayCol(DataGridView dgv, String dataPropertyName, String headerText, bool visibl)
        {
            dgv.AutoGenerateColumns = false;
            DataGridViewTextBoxColumn obj = new DataGridViewTextBoxColumn();
            obj.DataPropertyName = dataPropertyName;
            obj.HeaderText = headerText;
            obj.Name = dataPropertyName;
            obj.Visible = visibl;
            obj.Resizable = DataGridViewTriState.True;
            dgv.Columns.AddRange(new DataGridViewColumn[] { obj });
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Minimized;
                this.Hide();
            }
            else if (this.WindowState == FormWindowState.Minimized)
            {
                this.Show();
                this.WindowState = FormWindowState.Normal;
                this.Activate();
            }
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide(); //或者是this.Visible = false;
            }
        }

        private void exitMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("你确定要退出程序吗？", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                notifyIcon1.Visible = false;
                this.Close();
                this.Dispose();
                Application.Exit();
            }
        }

        private void hideMenuItem_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            this.Hide();
        }

        private void showMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }        

        private void timer2_Tick(object sender, EventArgs e)
        {
            BindIcon();
        }

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        extern static bool DestroyIcon(IntPtr handle);

        private void BindIcon()
        {
            Bitmap btm1 = new Bitmap(this.imageList1.Images[0]);
            Icon icon1 = Icon.FromHandle(btm1.GetHicon());
            Bitmap btm2 = new Bitmap(this.imageList1.Images[1]);
            Icon icon2 = Icon.FromHandle(btm2.GetHicon());

            if (i < 1)
            {
                this.notifyIcon1.Icon = icon1;
                i = 1;
            }
            else
            {
                this.notifyIcon1.Icon = icon2;
                i = 0;
            }
            DestroyIcon(icon2.Handle);
            DestroyIcon(icon1.Handle);            
        }

        private void NoBindIcon()
        {
            this.timer2.Enabled = false;
            this.notifyIcon1.Icon = this.Icon;
        }
    }
}
